﻿Imports System.Data.SqlClient
Public Class frmDieselBorrar

    Private Sub frmDieselBorrar_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        datFecha1.Value = Now()
        datFecha2.Value = Now()
        dgDiesel.Rows.Clear()
        chkTodos.Checked = False


    End Sub
    Sub CargaDatos()

        dgDiesel.Rows.Clear()

        Dim strFecha1 As String = datFecha1.Value.ToString("MM/dd/yyyy")
        Dim strFecha2 As String = datFecha2.Value.ToString("MM/dd/yyyy")
        '--------
        Dim cnconn As New SqlConnection
        cnconn.ConnectionString = CitraConnection
        Dim strSql As String = ""
        cnconn.Open()
        Dim cmdBuscar As New SqlCommand
        cmdBuscar.Connection = cnconn
        Dim cmdAsignar As New SqlCommand
        cmdAsignar.Connection = cnconn

        strSql = "SELECT IdDiesel,A.IdAutobus,cCodigoC01,A.NoEconomico,Fecha,Hora,Litros,KilometrosTotales FROM Diesel D INNER JOIN Autobuses A ON D.IdAutobus=A.IdAutobus WHERE CONVERT(VARCHAR,Fecha,101)>='" & strFecha1 & "' AND CONVERT(VARCHAR,Fecha,101)<='" & strFecha2 & "' ORDER BY Fecha,IdDiesel "
        cmdBuscar.CommandText = strSql
        Dim rdBuscar As SqlDataReader
        rdBuscar = cmdBuscar.ExecuteReader

        Dim fecha As Date
        Dim registro As Date

        Do While rdBuscar.Read()
            fecha = rdBuscar("Fecha")
            registro = rdBuscar("Hora")
            dgDiesel.Rows.Add(rdBuscar("IdDiesel"), True, rdBuscar("cCodigoC01").ToString.Trim, rdBuscar("NoEconomico").ToString.Trim, registro.ToShortDateString, fecha.ToShortDateString, rdBuscar("Litros"), rdBuscar("KilometrosTotales"))
            Application.DoEvents()
        Loop

        chkTodos.Checked = True

    End Sub

    Private Sub chkTodos_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkTodos.CheckedChanged
        Dim i As Integer

        If chkTodos.Checked = True Then
            For i = 0 To dgDiesel.Rows.Count - 1
                dgDiesel.Rows(i).Cells(1).Value = True
            Next
        Else
            For i = 0 To dgDiesel.Rows.Count - 1
                dgDiesel.Rows(i).Cells(1).Value = False
            Next
        End If
    End Sub

    Private Sub RadButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadButton2.Click
        dgDiesel.Rows.Clear()

    End Sub

    Private Sub RadButton4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadButton4.Click
        Close()

    End Sub

    Private Sub RadButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadButton3.Click
       

        Dim i As Integer
        Dim Kilometros As Double
        Dim dias As Integer
        Dim idCte As String

        For i = 0 To dgDiesel.Rows.Count - 1
            If dgDiesel.Rows(i).Cells(1).EditedFormattedValue = True Then
                If DiaTrabajadoDiesel(CDate(dgDiesel.Rows(i).Cells(5).Value)) = True Then
                    MsgBox("El dia " & CDate(dgDiesel.Rows(i).Cells(5).Value) & " ya ha sido archivado", MsgBoxStyle.Exclamation, "Aviso")
                Else
                    dgDiesel.CurrentCell = dgDiesel.Rows(i).Cells(1)
                    Application.DoEvents()
                    'Ahora actualiza odometros y dias
                    Kilometros = dgDiesel.Rows(i).Cells(7).Value
                    If Kilometros = 0 Then
                        dias = 0
                    ElseIf Kilometros <= 350 And Kilometros > 0 Then
                        dias = 2
                    ElseIf Kilometros > 350 And Kilometros <= 700 Then
                        dias = 3
                    ElseIf Kilometros > 700 Then
                        dias = 4
                    End If
                    idCte = dgDiesel.Rows(i).Cells(3).Value
                    Call SetSqlCitra("Update Autobuses set Importe01=Importe01-" & Kilometros & " where NoEconomico='" & idCte & "'")
                    Call SetSqlCitra("Delete From Diesel where IdDiesel=" & dgDiesel.Rows(i).Cells(0).Value)
                End If
                
            End If
        Next
        CargaDatos()
        MsgBox("Los movimientos seleccionados han sido eliminados", MsgBoxStyle.Information, "Aviso")


    End Sub
    Private Sub RadButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadButton1.Click
        CargaDatos()

    End Sub
End Class